package com.hrm.controller;


import com.github.pagehelper.Page;
import com.hrm.pojo.Employee;
import com.hrm.request.FindEmployeeRequest;
import com.hrm.service.EmployeeService;
import com.hrm.utils.entity.PageResult;
import com.hrm.utils.entity.Result;
import com.hrm.utils.entity.StatusCode;
import com.hrm.vo.FindEmployeeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/employee")
@Api(tags = "员工管理模块的接口")
@Transactional
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 分页查询
     * @param findEmployeeRequest
     * @return
     */
    @PostMapping("/find")
    @ApiOperation(value = "查询员工列表接口")
    public Result findEmployeeByInformations(@RequestBody FindEmployeeRequest findEmployeeRequest) throws Exception {

       Page<FindEmployeeVo> page=  employeeService.findEmployeeByInformations(findEmployeeRequest);
        PageResult<FindEmployeeVo> pageResult=new PageResult<FindEmployeeVo>(page.getTotal(),page.getResult());
        Result result = new Result(page != null, page != null ? StatusCode.SUCCESS : StatusCode.ERROR, "操作正常完成", pageResult);
        return result;

    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加员工接口")
    public Result addEmployee(@RequestBody Employee employee){
       boolean flag = employeeService.addEmployee(employee);
        Result result = new Result(flag, flag ? StatusCode.SUCCESS : StatusCode.ERROR, flag ? "添加成功":"添加失败", null);
        return result;
    }

    /**
     * 查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/query/{id}")
    @ApiOperation(value = "查询员工信息接口")
    public Result findEmployeeById(@PathVariable() Integer id ){
        Employee employee = employeeService.queryEmployee(id);
        Result result = new Result(employee!=null,employee!=null ? StatusCode.SUCCESS : StatusCode.ERROR,employee!=null?"查询到一条记录":"查无记录",employee);
        return result;
    }

    /**
     * 修改用户信息
     * @param employee
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改员工信息接口")
    public Result updateEmployee(@RequestBody Employee employee){
        boolean flag = employeeService.updateEmployee(employee);
        Result result = new Result(flag,flag ? StatusCode.SUCCESS : StatusCode.ERROR,flag ? "修改成功":"修改失败");
        return result;
    }

    /**
     * 删除一个或多个员工
     * @param employeeIdList
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除单个或多个员工信息接口")
    public Result deleteEmployee(@RequestBody ArrayList<Integer> employeeIdList){
        boolean flag = employeeService.deleteEmployees(employeeIdList);
        Result result = new Result(flag,flag ? StatusCode.SUCCESS : StatusCode.ERROR,flag ? "删除成功":"删除失败");
        return result;
    }







}
